package com.oa.android.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.oa.android.R;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 添加通讯录
 *
 * @author -zouqh
 */
public class AddTxlActivity extends Activity implements OnClickListener {


    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.Uname)
    EditText Uname;
    @InjectView(R.id.man)
    RadioButton man;
    @InjectView(R.id.wuman)
    RadioButton wuman;
    @InjectView(R.id.sexRadio)
    RadioGroup sexRadio;
    @InjectView(R.id.shereRadio)
    RadioGroup shereRadio;
    @InjectView(R.id.phone)
    EditText phone;
    @InjectView(R.id.emil)
    EditText emil;
    @InjectView(R.id.comName)
    EditText comName;
    @InjectView(R.id.comCell)
    EditText comCell;
    @InjectView(R.id.Qq)
    EditText qq;
    private String sex="男";
    private String str="1";
    ProgressDialog dialog;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_txl_activity);
        ButterKnife.inject(this);
        titleText.setText("添加通讯录");
        ritghText.setText("提交");
        ritghText.setVisibility(View.VISIBLE);
        close.setOnClickListener(this);
        ritghText.setOnClickListener(this);
        type=Integer.parseInt(getIntent().getStringExtra("type"));

        sexRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.man:
                        sex="男";
                        break;
                    case R.id.wuman:
                        sex="女";
                        break;
                }
            }
        });

        sexRadio.check(R.id.man);

        shereRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.no:
                        str="1";
                        break;
                    case R.id.yes:
                        str="0";
                        break;
                }
            }
        });
        shereRadio.check(R.id.no);



    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent;
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.ritgh_text:
                String string1=Uname.getText().toString();
                String string2=phone.getText().toString();
                String string3=emil.getText().toString();
                String string4=comName.getText().toString();
                String string5=comCell.getText().toString();
                String string6=qq.getText().toString();
                if (!Util.NoNull(string1)){
                    Toast.makeText(getApplicationContext(),"姓名不能为空",Toast.LENGTH_SHORT).show();
                }else if (!Util.NoNull(string2)){
                    Toast.makeText(getApplicationContext(),"电话不能为空",Toast.LENGTH_SHORT).show();
                }else if (!Util.NoNull(string3)){
                    Toast.makeText(getApplicationContext(),"邮件不能为空",Toast.LENGTH_SHORT).show();
                }else if (!Util.isEmail(string3)){
                    Toast.makeText(getApplicationContext(),"邮箱格式不对",Toast.LENGTH_SHORT).show();
                }else if (!Util.NoNull(string4)){
                    Toast.makeText(getApplicationContext(),"单位名称不能为空",Toast.LENGTH_SHORT).show();
                }else if (!Util.NoNull(string5)){
                    Toast.makeText(getApplicationContext(),"单位电话不能为空",Toast.LENGTH_SHORT).show();
                }else{
                    addTxl(string1,string2,string3,type,string4,string5,string6);
                }

                break;

            default:
                break;
        }
    }


    //添加通讯录
    public void addTxl(String name,String phone,String emil,int type,String comName,String comCel,String qq) {
        dialog = ProgressDialog.show(this, null, "添加中...", true, false);
        RequestParams params = new RequestParams(UConstants.ADD_TXL);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("XM", name);
        params.addBodyParameter("SEX", sex);
        params.addBodyParameter("MOBILE", phone);
        params.addBodyParameter("EMAIL", emil);
        params.addBodyParameter("DWMC", comName);
        params.addBodyParameter("DWDH", comCel);
        params.addBodyParameter("QQ", qq);
        params.addBodyParameter("IFSHARE", str);
        if (type==2){
            params.addBodyParameter("LX", "1");
        }else{
            params.addBodyParameter("LX", type+"");
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            dialog.dismiss();
                            Toast.makeText(x.app(), "新增成功！", Toast.LENGTH_LONG).show();
                            Intent intent=new Intent();
                            setResult(RESULT_OK,intent);
                            finish();
                        } else {
                            dialog.dismiss();
                            Toast.makeText(x.app(), "新增失败！", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        dialog.dismiss();
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "新增出错！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }


}
